package com.example.sls.dao.field;

import com.example.sls.pojo.field.FieldBookings;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 场地预约表(FieldBookings)表数据库访问层
 *
 * @author makejava
 * @since 2024-06-18 10:55:01
 */
public interface FieldBookingsDao {
  /**
   * 通过ID查询单条数据
   *
   * @param id 主键
   * @return 实例对象
   */
  @Select("select * from field_bookings where id=#{id}")
  FieldBookings queryById(Integer id);

  /**
   * 查询指定行数据
   *
   * @param fieldBookings 查询条件
   * @param pageSize      分页对象
   * @param offset        偏移量，起始处
   * @return 对象列表
   */
  @Select("select * from field_bookings where 1=1" +
          " and (id=#{fieldBookings.id} or (isnull(#{fieldBookings.id}) or #{fieldBookings.id}=0))" +
          " and (user_id=#{fieldBookings.userId} or (isnull(#{fieldBookings.userId}) or #{fieldBookings.userId}=0))" +
          " and (field_id=#{fieldBookings.fieldId} or (isnull(#{fieldBookings.fieldId}) or #{fieldBookings.fieldId}=0))" +
          " and (booking_date=#{fieldBookings.bookingDate} or (isnull(#{fieldBookings.bookingDate}) or #{fieldBookings.bookingDate}=''))" +
          " and (start_time=#{fieldBookings.startTime} or (isnull(#{fieldBookings.startTime}) or #{fieldBookings.startTime}=''))" +
          " and (end_time=#{fieldBookings.endTime} or (isnull(#{fieldBookings.endTime}) or #{fieldBookings.endTime}=''))" +
          " and (participants=#{fieldBookings.participants} or (isnull(#{fieldBookings.participants}) or #{fieldBookings.participants}=0))" +
          " and (need_equipment=#{fieldBookings.needEquipment} or (isnull(#{fieldBookings.needEquipment}) or #{fieldBookings.needEquipment}=0))" +
          " and (created_at=#{fieldBookings.createdAt} or (isnull(#{fieldBookings.createdAt}) or #{fieldBookings.createdAt}=''))" +
          " limit #{pageSize} offset #{offset}")
  List<FieldBookings> queryAllByLimit(FieldBookings fieldBookings, Integer pageSize, Integer offset);

  /**
   * 统计总行数
   *
   * @return 总行数
   */
  @Select("select count(*) from field_bookings")
  long count();

  /**
   * 新增数据
   *
   * @param fieldBookings 实例对象
   * @return 影响行数
   */
  @Insert("insert into field_bookings (id,user_id,field_id,booking_date,start_time,end_time,participants,need_equipment,created_at) " +
          "values(" +
          "if(#{id}!=0 and !isnull(#{id}),#{id},null)," +
          "if(#{userId}!=0 and !isnull(#{userId}),#{userId},null)," +
          "if(#{fieldId}!=0 and !isnull(#{fieldId}),#{fieldId},null)," +
          "if(#{bookingDate}!='' and !isnull(#{bookingDate}),#{bookingDate},null)," +
          "if(#{startTime}!='' and !isnull(#{startTime}),#{startTime},null)," +
          "if(#{endTime}!='' and !isnull(#{endTime}),#{endTime},null)," +
          "if(#{participants}!=0 and !isnull(#{participants}),#{participants},null)," +
          "if(#{needEquipment}!=0 and !isnull(#{needEquipment}),#{needEquipment},null)," +
          "if(#{createdAt}!='' and !isnull(#{createdAt}),#{createdAt},null)" +
          ")")
  int insert(FieldBookings fieldBookings);

  /**
   * 通过id修改数据
   * id 主键
   *
   * @param fieldBookings 实例对象
   * @return 影响行数
   */
  @Update("update field_bookings set " +
          "user_id=if(isnull(#{userId}) or #{userId}=0,user_id,#{userId})," +
          "field_id=if(isnull(#{fieldId}) or #{fieldId}=0,field_id,#{fieldId})," +
          "booking_date=if(isnull(#{bookingDate}) or #{bookingDate}='',booking_date,#{bookingDate})," +
          "start_time=if(isnull(#{startTime}) or #{startTime}='',start_time,#{startTime})," +
          "end_time=if(isnull(#{endTime}) or #{endTime}='',end_time,#{endTime})," +
          "participants=if(isnull(#{participants}) or #{participants}=0,participants,#{participants})," +
          "need_equipment=if(isnull(#{needEquipment}) or #{needEquipment}=0,need_equipment,#{needEquipment})," +
          "created_at=if(isnull(#{createdAt}) or #{createdAt}='',created_at,#{createdAt})" +
          " where id=#{id}")
  int update(FieldBookings fieldBookings);

  /**
   * 通过主键删除数据
   *
   * @param id 主键
   * @return 影响行数
   */
  @Delete("delete from field_bookings where id=#{id}")
  int deleteById(Integer id);
}

